package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarriagetManageBinding;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.BusinessSubscribeEntity;
import com.sgb.kjwl.model.entity.self.MyOfferEntity;
import com.sgb.kjwl.view.ui.activity.transport.driver.goods.FreightPrjDetailActivity;
import com.sgb.kjwl.view.ui.activity.transport.driver.goods.GeneralPrjDetailActivity;
import com.sgb.kjwl.view.ui.adapter.self.carMasters.SourceSubscriptionAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.carMakers.BusinessOpportunitySubscriptionModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * 商机订阅列表
 */
public class BusinessOpportunitySubscriptionActivity extends AppActivity {

    BusinessOpportunitySubscriptionModel viewModel;
    ActivityCarriagetManageBinding binding;
    private SourceSubscriptionAdapter sourceAdapter;
    private UserMsgEntity userMsgEntity;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BusinessOpportunitySubscriptionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_carriaget_manage);
        binding.include.rightView.setText("我的线路");
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.titleView.setText("货源订阅");
        binding.include.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        viewModel = new ViewModelProvider(this).get(BusinessOpportunitySubscriptionModel.class);

        if (sourceAdapter == null)
            sourceAdapter = new SourceSubscriptionAdapter(getActivity(), null);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divider_5dp));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(sourceAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setPullRefreshEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.myGoodsSource(BusinessOpportunitySubscriptionActivity.this, false);
            }

            @Override
            public void onLoadMore() {
                viewModel.myGoodsSource(BusinessOpportunitySubscriptionActivity.this, true);
            }
        });

        sourceAdapter.setOnItemClick(new AdapterOnItemClick<BusinessSubscribeEntity>() {
            @Override
            public void onItemClick(BusinessSubscribeEntity entity, int position) {
                if (entity != null)
                    if (Integer.parseInt(entity.getPattern())==1){
                        FreightPrjDetailActivity.start(getActivity(), entity.getNo(), false, false);
                    }else {
                        GeneralPrjDetailActivity.start(getActivity(), entity.getNo(), Integer.parseInt(entity.getPattern()), false);
                    }

            }
        });

//        viewModel.carsLiveDta.observe(this, carInfo -> showCarInfo(carInfo));
        viewModel.userMsgLiveDta.observe(this, userMsgEntity -> showAuth(userMsgEntity));
        viewModel.refreshLiveDta.observe(this, businessSubscribeEntities -> showList(businessSubscribeEntities));
        viewModel.loadMoreLiveDta.observe(this, businessSubscribeEntities -> loadMore(businessSubscribeEntities));
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.myGoodsSource(this, false);
        viewModel.getUserInfo(this);
//        viewModel.getCarInfo(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                if (userMsgEntity != null) {
                    switch (userMsgEntity.isDriverAuth() ? "SUCCESS" : "NONE") {
                        case "FAIL":
                            MToast.showToast(this, "司机认证不通过，不能进行操作");
                            break;
                        case "STANDBY":
                            MToast.showToast(this, "司机认证审核中，不能进行操作");
                            break;
                        default:
                            OwnPathsActivity.start(this);
                            break;
                    }

                }
                break;
        }
    }

/*    public void showDialog(String type) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        if (type.equals("driver")) {
            tv_title.setText("认证");
            tv_message.setText("请先进行司机认证再操作");
        } else if (type.equals("nocar")) {
            tv_title.setText("无可用车辆");
            tv_message.setText("请先添加车辆");
            tv_logout_confirm.setText("去添加");
        } else if (type.equals("driverFail")) {
            tv_title.setText("司机认证不通过");
            tv_message.setText("请重新提交认证信息");
        } else if (type.equals("driverStandby")) {
            tv_title.setText("司机认证审核中");
            tv_message.setText("认证通过后才能操作");
        }
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("driver")) {

                    Intent intent = new Intent(getActivity(), AddDriverInfoActivity.class);
                    startActivity(intent);
                } else if (type.equals("nocar")) {
                    AddCarActivity.start(BusinessOpportunitySubscriptionActivity.this);
                }

                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }*/

/*    public void showCarInfo(List<CarInfo> carInfo) {
        this.carInfo = carInfo;
    }*/

    public void showList(List<BusinessSubscribeEntity> list) {
        binding.recyclerView.refreshComplete();
        if (list == null)
            return;
        if (list.size() > 0) {
            sourceAdapter.setDatas(list);
            binding.recyclerView.setVisibility(View.VISIBLE);
            ;
            binding.emptyView.setVisibility(View.GONE);
        } else {
            sourceAdapter.setDatas(null);
            binding.emptyView.setVisibility(View.VISIBLE);
        }
    }

    public void loadMore(List<BusinessSubscribeEntity> list) {
        binding.recyclerView.loadMoreComplete();
        if (list == null)
            return;
        if (list.size() > 0) {
            sourceAdapter.addDatas(list);
        }
    }

    public void showDialog(MyOfferEntity myOfferEntity) {
        View view = View.inflate(getActivity(), R.layout.dialog_my_offer, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_come_time = view.findViewById(R.id.tv_come_time);
        TextView tv_price = view.findViewById(R.id.tv_price);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_connect = view.findViewById(R.id.tv_connect);
        TextView tv_phone = view.findViewById(R.id.tv_phone);
        TextView tv_remark = view.findViewById(R.id.tv_remark);
        tv_come_time.setText(DateUtils.stampToDay(myOfferEntity.getShipmentTime()));
        tv_price.setText(myOfferEntity.getFreightStr());
        tv_connect.setText(myOfferEntity.getContrcts());
        tv_phone.setText(myOfferEntity.getContrctsNumber());
        tv_remark.setText(myOfferEntity.getNote());
        tv_title.setText("我的报价");

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    public void showAuth(UserMsgEntity data) {
        this.userMsgEntity = data;
    }

    public void setEmptyVisibility() {
        binding.emptyView.setVisibility(View.VISIBLE);
    }
}
